<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class News extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Newsmodel','m_news',true);
    }

    public function index(){
        $data = parent::index();

        $data['attr'] = array();
        $data['attr']['news_image_width'] = IMAGE_SIZE_NEWS_WIDTH;
        $data['attr']['news_image_height'] = IMAGE_SIZE_NEWS_HEIGHT;

        $this->load->view('admin/news',$data);
    }

    public function getNewsList(){
        $assist['where'] = getFilter($this->input->post(),Newsmodel::$searchField);
        $assist['like'] = getFilter($this->input->post(),Newsmodel::$likeField);
        $assist['sort'] = getSort($this->input->post(),Newsmodel::$sortField);
        $assist['pagination'] = getPagination($this->input->post());

        $assist['join_content'] = true;

        list($list,$count) = $this->m_news->getNewsListSection($assist);

        foreach ($list as $k => $r) {
    			$list[$k]['news_image'] = empty($r['news_image']) ? '' : UPLOAD_URL_THUMB.$r['news_image'];
    		}

        $this->response->setSuccess();
        $this->response->setItems($list);
        $this->response->setTotalCount($count);
        $this->response->responseExit();
    }

    public function createNews(){
      $this->validate->setRule('news_title','string');
      $this->validate->setRule('news_desc','string');
      $this->validate->setRule('news_image','file');
      $this->validate->setRule('news_content','editor');
      if(!$this->validate->run($this->input->post())){
        $this->response->setDetail($this->validate->getMessage());
        $this->response->responseExit();
      }
      $info = $this->validate->getResult();

      $news_content = $info['news_content'];
      unset($info['news_content']);

      $info['news_image'] = $this->m_app->saveFile($info['news_image'],array(
        'width' => IMAGE_SIZE_NEWS_WIDTH,
        'height' => IMAGE_SIZE_NEWS_HEIGHT,
        'scale_width' => IMAGE_SIZE_DEFAULT_THUMB_WIDTH,
      ));

      $info['user_id'] = $this->m_app->getCurrentUserId();
      $info['news_status'] = STATUS_PENDING;
      $info['news_time_create'] = NOW;
      $news_id = $this->m_news->createNews($info);
      $this->m_news->createOrEditNewsContent(array(
        'news_id' => $news_id,
        'news_content' => $news_content,
      ));

      $this->response->setSuccess();
      $this->response->responseExit();
    }

    public function editNews(){
  		$this->validate->setRule('news_id','integer');
      $this->validate->setRule('news_title','string');
      $this->validate->setRule('news_desc','string');
      $this->validate->setRule('news_image','file');
  		$this->validate->setRule('news_image','optional');
      $this->validate->setRule('news_content','editor');
  		if(!$this->validate->run($this->input->post())){
  			$this->response->setDetail($this->validate->getMessage());
  			$this->response->responseExit();
  		}
  		$info = $this->validate->getResult();

      $news_content = $info['news_content'];
      unset($info['news_content']);

  		if(!empty($info['news_image'])){
  			$info['news_image'] = $this->m_app->saveFile($info['news_image'],array(
          'width' => IMAGE_SIZE_NEWS_WIDTH,
          'height' => IMAGE_SIZE_NEWS_HEIGHT,
          'scale_width' => IMAGE_SIZE_DEFAULT_THUMB_WIDTH,
  			));
  		}

  		$info['user_id'] = $this->m_app->getCurrentUserId();
  		$this->m_news->editNews($info);


      $this->m_news->createOrEditNewsContent(array(
        'news_id' => $info['news_id'],
        'news_content' => $news_content,
      ));


      $this->response->setSuccess();
  		$this->response->responseExit();
  	}


	public function disableNews(){
		$this->validate->setRule('news_id','integer');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		$info['user_id'] = $this->m_app->getCurrentUserId();
		$info['news_status'] = STATUS_DISABLE;

		$this->m_news->editNews($info);


		$this->response->setSuccess();
		$this->response->responseExit();
	}

	public function activeNews(){
		$this->validate->setRule('news_id','integer');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		$info['user_id'] = $this->m_app->getCurrentUserId();
		$info['news_status'] = STATUS_ACTIVE;

		$this->m_news->editNews($info);


		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

  public function deleteNews(){
		$this->validate->setRule('news_id','integer');
    if(!$this->validate->run($this->input->post())){
      $this->response->setDetail($this->validate->getMessage());
      $this->response->responseExit();
    }
    $info = $this->validate->getResult();
    $info['user_id'] = $this->m_app->getCurrentUserId();
    $info['news_status'] = STATUS_DELETE;

    $this->m_news->editNews($info);


    $this->response->setSuccess();
    $this->response->responseExit();
  }

  public function homeNews() {
    $this->validate->setRule('news_id','integer');
    if(!$this->validate->run($this->input->post())){
      $this->response->setDetail($this->validate->getMessage());
      $this->response->responseExit();
    }
    $info = $this->validate->getResult();

    $this->m_news->updateNewsHome($info['news_id']);


    $this->response->setSuccess();
    $this->response->responseExit();
  }

  public function editNewsSort(){
    $this->validate->setRule('news_id','integer');
    $this->validate->setRule('news_sort','integer');
    if(!$this->validate->run($this->input->post())){
      $this->response->setDetail($this->validate->getMessage());
      $this->response->responseExit();
    }
    $info = $this->validate->getResult();

    $this->load->database();
    $this->m_news->editNews(array(
      'news_id' => $info['news_id'],
      'user_id' => $this->m_app->getCurrentUserId(),
      'news_sort' => $info['news_sort'],
    ));
    $this->db->close();

    $this->response->setSuccess();
    $this->response->responseExit();
  }
}
/* End of file api_log.php */
/* Location: ./application/controllers/api_log.php */
